package kr.co.graviti.adm.bbs.action;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import kr.co.graviti.adm.common.dao.AdmCommonDAO;
import kr.co.graviti.common.action.BaseFuncAction;
import kr.co.graviti.common.util.CommonUtil;
import kr.co.graviti.common.util.PagingUtil;

public class AdmBbsListAction extends BaseFuncAction{
	private static final Logger _log = Logger.getLogger(AdmBbsListAction.class);
	
	/** 게시판 SAMPLE
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward notice(ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,HttpServletResponse response)
	        throws Exception {
		
		boolean _result = new AdmBbsListAction().boardPagingList(request, response);
		String returnfoward = "";
		if(_result){
			returnfoward = CommonUtil.strNullChk(request.getParameter("board_type"));
		}else{
			returnfoward = "fail";
		}
		System.out.println("===== 공지사항  ========");
		return mapping.findForward(returnfoward);
	}
	
	public ActionForward news(ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,HttpServletResponse response)
	        throws Exception {
		
		boolean _result = new AdmBbsListAction().boardPagingList(request, response);
		String returnfoward = "";
		if(_result){
			returnfoward = CommonUtil.strNullChk(request.getParameter("board_type"));
		}else{
			returnfoward = "fail";
		}
		System.out.println("===== NEWS ========");
		return mapping.findForward(returnfoward);
	}
	
	
	/** 관리자 게시판 페이징 리스트 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public boolean boardPagingList(HttpServletRequest request,HttpServletResponse response) throws Exception{
		
		boolean _result = true;
		
		HashMap req = null;
		HashMap bbsSort = new HashMap();
		req = CommonUtil.getRequest(request);
		String ibatisNm = "admBbs";
		
		int currentPage = Integer.parseInt(CommonUtil.strNullChk((String)req.get("currentPage"),"0"));
		int currentGroup = Integer.parseInt(CommonUtil.strNullChk((String)req.get("currentGroup"),"0"));
		
		if (currentPage == 0) currentPage = 1; 
		if (currentGroup == 0) currentGroup = 1;
		
		int page_per_navigator = 10;	// 리스트 갯수  
		int page_per_groupsu = 10;		// 페이증 그룹 갯수 
		
		int startNum = (page_per_navigator * (currentPage -1)) + 1;
		int endNum	 = page_per_navigator * currentPage;
		
		AdmCommonDAO dao = new AdmCommonDAO();
		/* 게시판 기본정보 */
		int bbsCnt = dao.getIntegerDAO(req, ibatisNm+".selectBbsSortCount");
		if(bbsCnt > 0){
			bbsSort = dao.getHashMapDAO(req, ibatisNm+".selectBbsSort");
			bbsSort.put("page_per_navigator", Integer.toString(page_per_navigator));
			req.put("board_cd", bbsSort.get("BOARD_CD"));
			req.put("lang", bbsSort.get("LANG"));
		}else{
			String alertMsg = CommonUtil.printAlertBack("잘못된 페이지 정보 입니다..");
			
			request.setAttribute("alertMsg", alertMsg);
			_result = false;
		}
		
		req.put("qorder", "order by top_notice_YN asc, related_date desc,board_idx desc");
		req.put("startNum", Integer.toString(startNum));
		req.put("endNum", Integer.toString(endNum));
		
		//검색열 
		req.put("searchkind", CommonUtil.strNullChk((String)req.get("searchkind"),""));
		req.put("searchword", CommonUtil.strNullChk((String)req.get("searchword"),""));
		
		_log.info("HashMap===>>"+req);
		
		int bbsTotalCnt = dao.getIntegerDAO(req, ibatisNm + ".selectBbsTotalCnt");
		List bbsPagingList = dao.getArrayListDAO(req, ibatisNm + ".selectBbsPagingList");
		
		// maxThreadIdxOfList 값 가져오기
		int maxThreadIdxOfList = dao.getIntegerDAO(req, ibatisNm + ".selectMaxThreadidxOfList");
		
		req.put("totArticle",Integer.toString(bbsTotalCnt));
		req.put("maxThreadIdxOfList",Integer.toString(maxThreadIdxOfList));
		req.put("currentPage",Integer.toString(currentPage));
		req.put("currentGroup",Integer.toString(currentGroup));
		req.put("page_per_navigator", Integer.toString(page_per_navigator));
		req.put("page_per_groupsu", Integer.toString(page_per_groupsu));
		
		StringBuffer sb = new PagingUtil(req).getPagingUtil();
		String paging = sb.toString();
		
		request.setAttribute("req", req);
		request.setAttribute("bbsPagingList", bbsPagingList);
		request.setAttribute("paging", paging);
		request.setAttribute("bbsSort", bbsSort);
		
		return _result;
	}
	
	
	/** 관리자 게시판 순서조정 처리
	 * @param request
	 * @param response
	 * @param board_idx				게시판 일련번호
	 * @param change_thread_idx		변경된 게시판 thread_idx 일련번호
	 * @return
	 * @throws Exception
	 */
	public ActionForward boardTableOrderProc(ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,HttpServletResponse response)
	        throws Exception {
		
		System.out.println("====================boardTableOrderProc()");

		HashMap req = new HashMap();
		req = CommonUtil.getRequest(request);
		String ibatisNm = "admBbs";
		AdmCommonDAO dao = new AdmCommonDAO();
		
		
		String [] str_board_idx_arr = request.getParameterValues("board_idx");
		int [] board_idx = new int[3];
		for(int i = 0; i < str_board_idx_arr.length; i++ ){
			board_idx[i] = Integer.parseInt(str_board_idx_arr[i]);
		}
		
		String [] str_change_thread_idx_arr = request.getParameterValues("change_thread_idx");
		int [] change_thread_idx = new int [3];
		for(int i = 0; i < str_change_thread_idx_arr.length; i++ ){
			change_thread_idx[i] = Integer.parseInt(str_change_thread_idx_arr[i]);
		}
		
		for (int i = 0; i < board_idx.length; i ++) {
			_log.info("board_idx : " + board_idx[i]);
			_log.info("change_thread_idx : " + change_thread_idx[i]);
		}
		
		for (int i = 0; i < board_idx.length; i ++) {
			req.put("board_idx", Integer.toString(board_idx[i]));
			req.put("change_thread_idx", Integer.toString(change_thread_idx[i]));
			
			dao.setUpdateDAO(req, ibatisNm + ".updateIrFaqReorder"); 
		}
		
		request.setAttribute("mode", "irfaq");
		
		return mapping.findForward("proc");
	}
}
